package com.jpcc.vw.onlineRadio.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.activity.MainActivity;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.utils.ToastUtils;
import com.orhanobut.logger.Logger;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NetworkChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Logger.i("networkChangeReceiver-->" + "onReceive");
//        boolean networkAvailable=NetworkUtils.isNetworkAvailable(context);
//        Observable.create(new Observable.OnSubscribe<Boolean>() {
//            @Override
//            public void call(Subscriber<? super Boolean> subscriber) {
//                Socket s = null;
//                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
//                boolean connected = (null != activeNetworkInfo) && activeNetworkInfo.isConnected();
//                Log.d("lubin", "NetworkInfo active = " + connected);
//                if (!connected) {
//                    subscriber.onNext(false);
//                    return;
//                }
//                boolean routeExists;
//                try {
//                    if (s == null) {
//                        s = new Socket();
//                    }
//                    InetAddress host = InetAddress.getByName("8.8.8.8");//全球通用google：8.8.8.8
//                    s.connect(new InetSocketAddress(host, 53), 5000);//google:53
//                    routeExists = true;
//                } catch (IOException e) {
//                    Log.d("lubin", "IOException = " + e.getMessage());
//                    routeExists = false;
//                } finally {
//                    try {
//                        s.close();
//                        s = null;
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//                boolean bConnect = connected && routeExists;
//                Log.d("lubin", "net connect = " + bConnect);
//                subscriber.onNext(connected && routeExists);
////                return connected && routeExists;
//            }
//        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<Boolean>() {
//            @Override
//            public void onCompleted() {
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//            @Override
//            public void onNext(Boolean flag) {
//                if (!flag) {
//                    ToastUtils.show(R.string.string_51);
////                    Logger.i("networkChangeReceiver-->" + NetworkUtils.isNetworkAvailable(context));
//                    if(AlbumRadioAudioPlayer.get().isPlaying()){
//                        AlbumRadioAudioPlayer.get().pausePlayer();
//                    }
//                }
//                else{
////                    Logger.i("networkChangeReceiver-->" + networkAvailable);
//                    ToastUtils.show("Network recharged");
//                    if(!AlbumRadioAudioPlayer.get().isPlaying()){
//                        AlbumRadioAudioPlayer.get().playPause();
//                    }
//                }
//            }
//        });


        if (Constants.isRelease) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            boolean connected = connectivityManager.getNetworkStatus();
            Logger.i("network Status-->" + connected);
            if (!connected) {
//                ToastUtils.show(R.string.string_51);
            }
        }


//        if (!networkAvailable) {
//            ToastUtils.show(R.string.string_51);
//            Logger.i("networkChangeReceiver-->" + NetworkUtils.isNetworkAvailable(context));
//            if(AlbumRadioAudioPlayer.get().isPlaying()){
//                AlbumRadioAudioPlayer.get().pausePlayer();
//            }
//        }
//        else{
//            Logger.i("networkChangeReceiver-->" + networkAvailable);
//            ToastUtils.show("Network recharged");
//            if(!AlbumRadioAudioPlayer.get().isPlaying()){
//                AlbumRadioAudioPlayer.get().playPause();
//            }
//        }
    }
}
